package com.vert.flowable;

import com.vert.utils.ObjectMaps;
import com.vert.utils.PropertiesUtils;
import org.flowable.engine.*;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;

/**
 * @Classname FlowableUtils
 * @Description TODO
 * @Date 2022/5/29 9:27
 * @Created by XiongXiong
 * @Author: X.I.O
 */
public class FlowableUtils {

    public static void init(){
          String url= PropertiesUtils.getProperty("url");
          String driver=PropertiesUtils.getProperty("driver");
          String username=PropertiesUtils.getProperty("username");
          String password=PropertiesUtils.getProperty("password");
          ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
            .setJdbcUrl(url)
            .setJdbcUsername(username)
            .setJdbcPassword(password)
            .setJdbcDriver(driver)
            .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
          ProcessEngine processEngine = cfg.buildProcessEngine();
          ObjectMaps.setAutuWirdemap(ProcessEngine.class.getName(),processEngine);
    }

    public static ProcessEngine getProcessEngine() {
         ProcessEngine processEngine=(ProcessEngine)ObjectMaps.getObjectByClass(ProcessEngine.class);
         return processEngine;
    }

    public static TaskService getTaskService() {
        ProcessEngine processEngine=getProcessEngine();
        if(processEngine!=null){
            return processEngine.getTaskService();
        }
        return null;
    }

    public static RuntimeService getRuntimeService() {
        ProcessEngine processEngine = getProcessEngine();
        if (processEngine != null) {
            return processEngine.getRuntimeService();
        }
        return null;
    }

    public static HistoryService getHistoryService() {
        ProcessEngine processEngine = getProcessEngine();
        if (processEngine != null) {
            return processEngine.getHistoryService();
        }
        return null;
    }

    public static RepositoryService getRepositoryService() {
      ProcessEngine processEngine = getProcessEngine();
      if (processEngine != null) {
        return processEngine.getRepositoryService();
      }
      return null;
    }


//    public static ProcessEngine getProcesEngine(){
//        String url= PropertiesUtils.getProperty("url");
//        String driver=PropertiesUtils.getProperty("driver");
//        String username=PropertiesUtils.getProperty("username");
//        String password=PropertiesUtils.getProperty("password");
//        ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
//          .setJdbcUrl(url)
//          .setJdbcUsername(username)
//          .setJdbcPassword(password)
//          .setJdbcDriver(driver)
//          .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
//        ProcessEngine processEngine = cfg.buildProcessEngine();
//        return processEngine;
//    }
}
